$(function () {
    let id = localStorage.getItem("productId");
    findProductClassify();
    selectProductById(id);
})
//查询一条数据库
function selectProductById(id) {
    $.ajax({
        data:{id: id},
        url:'/back/product/updateProduct',
        dataType:'json',
        type:'get',
        success:function (data) {
            setData(data.productModel);
        }
    })
}
function setData(data) {
    $("#product-name").val(data.name);
    $("#product-description").val(data.content);
    $("#diliver").val(data.diliver);
    $("#averageprice").val(data.averageprice);
    $("#price").val(data.price);
    $("#quality").val(data.quality);
    $("#payment").val(data.payment);
    $("#information").val(data.information);
    $("#productClassify").val(data.classify);
}

function findProductClassify() {
    $.ajax({
        data:'',
        dataType:'json',
        type:'get',
        url:'/back/product/findProductClassify',
        success:function (data) {
            data=data.model;
            console.log(data);
            let html='';
            for (let i=0;i<data.length;i++){
                html +=' <option value="'+data[i].id+'">' + data[i].name + '</option>';
            };
            $("#productClassify").html(html);
        }
    })
}
//更新数据===================================================
function updateProduct() {
    let productid = localStorage.getItem("productId");
    updateProductById(productid);
}
function updateProductById(id) {
    let data = new FormData();
    data.append("classify",$("#productClassify").val());
    data.append("name",$("#product-name").val());
    data.append("img",$("#product-img")[0].files[0]);
    data.append("content",$("#product-description").val());
    data.append("diliver",$("#diliver").val());
    data.append("averageprice",$("#averageprice").val());
    data.append("price",$("#price").val());
    data.append("quality",$("#quality").val());
    data.append("payment",$("#payment").val());
    data.append("information",$("#information").val());
    data.append("id",id);
    $.ajax({
        data:data,
        dataType:'json',
        url:'/back/product/updateProduct',
        type:'post',
        contentType:false ,
        processData: false,
        success:function (data) {
            // let url = window.location.href;
            data = data.result;
            if (data==1){
                alert("更新成功");
            }else {
                alert("更新失败");
            }
            $("#right").load("/back/html/product/productUpdate.html");
        }
    });

}
//返回上一页面。。。。。。。。
function back() {
    $("#right").load("/back/html/product/productManage.html");
}
